package battleship;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 
 * @author John Venable
 * @version 1.1
 */

public class Game {
	
	private battleship.model.Grid playerGrid;
	private battleship.model.Grid computerGrid;
	private battleship.ai.Computer computer;
	
	public Game () {
		playerGrid = new battleship.model.Grid();
		computerGrid = new battleship.model.Grid();
		new battleship.model.ShipPlace(playerGrid);
		new battleship.model.ShipPlace(computerGrid);
		computer = new battleship.ai.Computer(playerGrid);
	}
	
	public Color setColor (int x, int y) {
		if(playerGrid.CheckOccupied(x, y)) {
			return Color.GRAY;
		} else {
			return Color.CYAN;
		}
	}

	public boolean processPlayerTurn(int x, int y, ActionEvent ae) {
	    if (computerGrid.Attack(x, y)) {
	        ((JButton)ae.getSource()).setBackground(Color.RED);
	        return true;
	    } else {
	    	((JButton)ae.getSource()).setBackground(Color.WHITE);
	    	return false;
	    }
	}
	
	public battleship.model.Shot processComputerTurn() {
		battleship.model.Shot thisAttack = computer.userAttack();
		return thisAttack;
	}
	
	public String getShip(battleship.model.Shot shot) {
		return playerGrid.TheGrid[shot.x][shot.y].shipName();
	}
	
	public boolean computerSunk() {
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 10; j++){
				if(computerGrid.TheGrid[i][j].IsOccupied() && !computerGrid.TheGrid[i][j].Hit())
					return false;
			}
		}
		return true;
	}
	
	public boolean playerSunk() {
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 10; j++){
				if(playerGrid.TheGrid[i][j].IsOccupied() && !playerGrid.TheGrid[i][j].Hit())
					return false;
			}
		}
		return true;
	}
}
